<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Xangdau_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}
	
	
	/*
	 * Added by ManhVT
	 * Added date: 03/11/2014
	 * Thêm tải file báo cáo
	 */
	function get_report_tong_hopc($bdate,$edate)
	{
		
	}
	function get_ps_theo_banle()
	{
		
		$xangs = array();
		
			
			$this->db->select('o_chung_tu.*, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount, DATE(FROM_UNIXTIME(o_chung_tu.created_date)) as d');
				$this->db->from('o_chung_tu');
				$this->db->where('o_chung_tu.status', 1);
				$this->db->where('o_chung_tu.thiet_bi_id', 36);		
				$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
				$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
				$this->db->group_by('d');
				$query = $this->db->get();		
				$xangs = $query->result_array();
				
			
		
		
		$return['xang'] = $xangs;
		//var_dump($xangs); exit();
		$daus = array();
		
			$this->db->select('o_chung_tu.*, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount, DATE(FROM_UNIXTIME(o_chung_tu.created_date)) as d');
				$this->db->from('o_chung_tu');
				$this->db->where('o_chung_tu.status', 1);
				$this->db->where('o_chung_tu.thiet_bi_id', 50);
				$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
				$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
				$this->db->group_by('d');
				$query = $this->db->get();		
				$daus = $query->result_array();
			
		
		$return['dau'] = $daus;
		return $return;
	}
	function get_ps_theo_xe_dao_tao()
	{
		$this->db->select('o_thiet_bi.*');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.kind',1);
		$this->db->where('o_thiet_bi.status',1);
		$query = $this->db->get();
		
		$rows = $query->result_array();
		$xangs = array();
		if($rows)
		{
			foreach($rows as $row)
			{
				$this->db->select('o_chung_tu.*, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
				$this->db->from('o_chung_tu');
				$this->db->where('o_chung_tu.status', 1);
				$this->db->where('o_chung_tu.thiet_bi_id', $row['id']);
				$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
				$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
				$query = $this->db->get();		
				$rss = $query->row();
				if($rss)
				{
					if($rss->total)
					{
						$row['total'] = $rss->total;
					}else{ $row['total'] = 0;}			
				}
				else
				{
					$row['total'] = 0;	
				}
				
				$xangs[] = $row;
			}
		}
		
		$return['xang'] = $xangs;
		//var_dump($xangs); exit();
		$this->db->select('o_thiet_bi.*');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.kind',2);
		$this->db->where('o_thiet_bi.status',1);
		$query = $this->db->get();
		
		$rows = $query->result_array();
		$daus = array();
		if($rows)
		{
			foreach($rows as $row)
			{
				$this->db->select('o_chung_tu.*, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
				$this->db->from('o_chung_tu');
				$this->db->where('o_chung_tu.status', 1);
				$this->db->where('o_chung_tu.thiet_bi_id', $row['id']);
				$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
				$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
				$query = $this->db->get();		
				$rss = $query->row();
				if($rss)
				{
					if($rss->total)
					{
						$row['total'] = $rss->total;
					}else{ $row['total'] = 0;}			
				}
				else
				{
					$row['total'] = 0;	
				}
				
				$daus[] = $row;
			}
		}
		$return['dau'] = $daus;
		return $return;
	}
	
	function get_ps_theo_xelanhdao()
	{
		
		$this->db->select('o_thiet_bi.*');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.kind',3);
		$this->db->where('o_thiet_bi.status',1);
		$query = $this->db->get();
		
		$rows = $query->result_array();
		$xangs = array();
		if($rows)
		{
			foreach($rows as $row)
			{
				$this->db->select('o_chung_tu.*, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
				$this->db->from('o_chung_tu');
				$this->db->where('o_chung_tu.status', 1);
				$this->db->where('o_chung_tu.thiet_bi_id', $row['id']);
				$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
				$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
				$query = $this->db->get();		
				$rss = $query->row();
				if($rss)
				{
					if($rss->total)
					{
						$row['total'] = $rss->total;
					}else{ $row['total'] = 0;}			
				}
				else
				{
					$row['total'] = 0;	
				}
				
				$xangs[] = $row;
			}
		}
		
		$return['xang'] = $xangs;
		//var_dump($xangs); exit();
		$this->db->select('o_thiet_bi.*');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.kind',6);
		$this->db->where('o_thiet_bi.status',1);
		$query = $this->db->get();
		
		$rows = $query->result_array();
		$daus = array();
		if($rows)
		{
			foreach($rows as $row)
			{
				$this->db->select('o_chung_tu.*, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
				$this->db->from('o_chung_tu');
				$this->db->where('o_chung_tu.status', 1);
				$this->db->where('o_chung_tu.thiet_bi_id', $row['id']);
				$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
				$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
				$query = $this->db->get();		
				$rss = $query->row();
				if($rss)
				{
					if($rss->total)
					{
						$row['total'] = $rss->total;
					}else{ $row['total'] = 0;}			
				}
				else
				{
					$row['total'] = 0;	
				}
				
				$daus[] = $row;
			}
		}
		$return['dau'] = $daus;
		return $return;
	}
	function get_ps_catcohc()
	{
		$ids = array('0');
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');	
		$this->db->where('o_chung_tu.status', 1);		
		$this->db->where('o_thiet_bi.kind', 7);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		if($row)
		{
			$return['xang'] = $row->giam;
		}else
		{
			$return['xang'] = 0;
		}
		
		$return['dau'] = 0;	
		return $return;
	}
	function get_ps_catcokhoaxe()
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');	
		$this->db->where('o_chung_tu.status', 1);		
		$this->db->where('o_thiet_bi.kind', 8);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		if($row)
		{
			$return['xang'] = $row->giam;
		}else
		{
			$return['xang'] = 0;
		}
		
		
		$return['dau'] = 0;
		
			
		return $return;
	}
	function get_ps_banle()
	{
		$ids = array('0');
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');	
		$this->db->where('o_chung_tu.status', 1);		
		$this->db->where('o_thiet_bi.kind', 0);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		if($row)
		{
			$return['xang'] = $row->giam;
		}else
		{
			$return['xang'] = 0;
		}
		
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_thiet_bi.kind', 9);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		
		if($row)
		{
			$return['dau'] = $row->giam;
		}else
		{
			$return['dau'] = 0;
		}
			
		return $return;
	}
	
	function get_ps_xehuanluyen()
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');	
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_thiet_bi.kind', 1); // huấn luyện xăng
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		if($row)
		{
			$return['xang'] = $row->giam;
		}else
		{
			$return['xang'] = 0;
		}
		
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_thiet_bi.kind', 2); // Huấn luyện dầu
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		
		if($row)
		{
			$return['dau'] = $row->giam;
		}else
		{
			$return['dau'] = 0;
		}
			
		return $return;
	}
	
	function get_ps_xelanhdao()
	{
		$ids = array('3', '6');
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');	
		$this->db->where('o_chung_tu.status', 1);		
		$this->db->where('o_thiet_bi.kind', 3); // Lãnh đạo Xăng
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		if($row)
		{
			$return['xang'] = $row->giam;
		}else
		{
			$return['xang'] = 0;
		}
		
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi','o_thiet_bi.id = o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_thiet_bi.kind', 6); // lãnh đạo dầu
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$row = $query->row();
		
		if($row)
		{
			$return['dau'] = $row->giam;
		}else
		{
			$return['dau'] = 0;
		}
			
		return $return;
	}
	
	
	function tao_bc_tong_hop()// Theo hạng mục như Xăng A95, Dầu Diezen, ... báo cáo tình trạng xuất nhập, còn trong kho
	{
		//1. Lấy danh mục đã submit
		$danhmucs = $this->get_all_category(1);
		$bc_tong_hop = $this->get_report_tong_hop_thang();
		if($bc_tong_hop) // Nếu có báo cáo, thì không làm gì; hiển thị báo cáo
		{
			return false;
		}
		// Nếu chưa có báo cáo, tạo báo cáo của tháng hiện thời
		// Lấy báo cáo của tháng trước
		$pre_bc_tong_hop = $this->get_report_tong_hop_thang_truoc();
		//var_dump($pre_bc_tong_hop);
		if($pre_bc_tong_hop == null) // Báo cáo tháng trước chưa có. Lấy khởi tạo hệ thống, số dư đầu ky
		{
			//2. Lấy danh sách đầu kỳ khởi tạo
			$khoi_tao = $this->get_all_sddk();
			//var_dump($khoi_tao); exit();
			foreach($danhmucs as $danhmuc)
			{
				foreach($khoi_tao as $j)
				{
					if($j['hang_muc_id']==$danhmuc['id'])
					{
						$this->add_o_tong_hop($danhmuc['id'],$j['sddk']);
					}
				}
			}
		}
		else
		{
			$newrows = array();
			$listdm = array();
			$listpbc = array();
			foreach($danhmucs as $danhmuc)
			{
				$flag = 0;
				foreach($pre_bc_tong_hop as $j)
				{
					if($j['hang_muc_id'] == $danhmuc['id'])
					{
						//a. cập nhật số dư cuối kỳ = sddk+ps tang - ps giảm
						$sum = $this->tinh_tong_ps_theo_hang_muc_thang_truoc($danhmuc['id']);
						if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
						if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;

						$j['sdck'] = $j['sddk']+$sum[0]['tang']- $sum[0]['giam'];
						// cập nhật ps tăng, ps giảm, sdck vào tong hop $j
						$this->update_o_tong_hop($j['id'],$sum[0]['tang'],$sum[0]['giam'],$j['sdck']);
						$this->add_o_tong_hop($danhmuc['id'],$j['sdck']);
					}
					
				}
				
				$listdm[$danhmuc['id']] = $danhmuc;
			}
			
			foreach($pre_bc_tong_hop as $j)
			{
				$listpbc[$j['hang_muc_id']] = $j;
			}
			$newrows = array_diff_key($listdm, $listpbc);
			// Nếu có danh mục mới thêm vào, cần lấy giá trị khởi tạo
			if($newrows)
			{
				$khoi_tao = $this->get_all_sddk();
				foreach($khoi_tao as $j)
				{
					
					foreach($newrows as $danhmuc)
					{
								
						if($j['hang_muc_id'] == $danhmuc['id'])
						{
							if($danhmuc['status'] == 1)
								{
									$this->add_o_tong_hop($danhmuc['id'],$j['sddk']);
								}
								else {
									$this->add_o_tong_hop($danhmuc['id'],0);
								}
						}
							
					}
				}
			}
			
		}
		return true;
	}
	
	function add_o_tong_hop($hangmucid,$sddk)
	{
	
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>0,
				'sddk'	=>$sddk,
				'ps_giam'		=> 0,
				'status' =>1,
				'kind' =>0,
				'thiet_bi_id'=>0
		);
		$this->db->set($fields);
		return $this->db->insert('o_tong_hop');
	
	}
	//////////////////////// END RP TONG HOP ///////////////
	 function get_report_chi_tiet_xe($itemId,$duration=null)
	 {
	 	if($itemId == null)
	 	{
	 		return null;
	 	}
	 	if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_chi_tiet_thang_xe($itemId); break;
			case 'THISYEAR':
				return $this->get_report_chi_tiet_nam_xe($itemId); break;
			case 'QUATER':
				return $this->get_report_chi_tiet_quy_xe($itemId); break;
			case 'HALF':
				return $this->get_report_chi_tiet_nua_xe($itemId); break;
			case 'LASTMONTH': 
				return $this->get_report_chi_tiet_thang_truoc_xe($itemId); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_chi_tiet_xe_c($itemId,$tmp[1],$tmp[2]); break;
		}
	 	
	}
	
	public function get_report_chi_tiet_xe_c($itemId,$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname, o_thiet_bi.id as tbid');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		$this->db->where('o_chung_tu.thiet_bi_id',$itemId);
		
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
			$this->db->where('o_chung_tu.created_date <=',$ngaykt);
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		
		return $rss;
	}
	
	public function get_report_chi_tiet_nam_xe($itemId)
	{
		
	}
	public function get_report_chi_tiet_quy_xe($itemId)
	{
		
	}
	public function get_report_chi_tiet_nua_xe($itemId)
	{
		
	}
	public function get_report_chi_tiet_thang_truoc_xe($itemId)
	{
		
	}
	public function get_report_chi_tiet_thang_xe($itemId)
	{
		$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname,o_thiet_bi.id as tbid, o_chung_tu.ps_giam, o_chung_tu.price');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		$this->db->where('o_chung_tu.thiet_bi_id',$itemId);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		$rss = $query->result_array();
		return $rss;
	}
	
	/*
	 * 
	 */
	function get_report_thcat($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_thcat_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam_xe(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy_xe(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua_xe(); break;
			case 'LASTMONTH': 
				return $this->get_report_tong_hop_thang_truoc_xe(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_thcat_c($tmp[1],$tmp[2]); break;
		}
	}
	/*
	 * type: 1 Xăng, 2 Dầu
	 * $cat: 1 đào tạo, 2 Lãnh đạo; 3 khác
	 */
	function get_report_thcat_thang_by_type($cate='',$type='')
	{
		$this->db->select('SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->where('o_chung_tu.status', 1);
		if($type != '')
		{
			$this->db->where('o_thiet_bi.type', $type);
		}
		if($cate != '')
		{
			$this->db->where('o_thiet_bi.category', $cate);
		}
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
	}
	
	function get_report_thcat_c_by_type($cate='',$type='',$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('SUM(o_chung_tu.ps_giam) AS total');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		if($type != '')
		{
			$this->db->where('o_thiet_bi.type',$type);
		}
		if($cate != '')
		{
			$this->db->where('o_thiet_bi.category',$cate);
		}
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
			$this->db->where('o_chung_tu.created_date <=',$ngaykt);
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		if($rss)
		{
			return $rss[0]['total'];
		}
		return 0;
	}
	
	/*
	 * Lấy danh sach xe theo cate và type
	 * - Danh sách xe đào tạo: cate = 1 đào tạo và chạy dầu: type =2
	 * - Danh sách xe tổng ôn: cate = 1 đào tạo; type = 1 chạy xăng
	 */	
	function get_listtb_by_cate_type($cate='',$type ='')
	{
		$this->db->select('o_thiet_bi.id, o_thiet_bi.name, o_thiet_bi.type');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.status', 1);
		$this->db->where('o_thiet_bi.category', $cate);
		$this->db->where('o_thiet_bi.type', $type);
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$return = array();
		if($rss)
		{
			foreach($rss as $item)
			{
				$item['nangluong'] = 0;				
				$return[$item['id']] = $item;
			}
		}
		return $return;
	}
	function get_listtb_by_cate($cate='')
	{
		$this->db->select('o_thiet_bi.id, o_thiet_bi.name, o_thiet_bi.type');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.status', 1);
		$this->db->where('o_thiet_bi.category', $cate);
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$return = array();
		if($rss)
		{
			foreach($rss as $item)
			{
				$item['xang'] = 0;
				$item['dau'] = 0;
				$return[$item['id']] = $item;
			}
		}
		return $return;
	}
	// Lay danh sach thiet bi voi cat=3
	function get_listtb_c3()
	{
		return $this->get_listtb_by_cate(3);
	}
	// Lay dianh muc voi cate = 3

	// Lay report xe phuc vu cong vu - lanh dao
	
	function get_report_dtdau($duration)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_thcat_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam_xe(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy_xe(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua_xe(); break;
			case 'LASTMONTH': 
				return $this->get_report_tong_hop_thang_truoc_xe(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_dtdau_c($tmp[1],$tmp[2]); break;
		}
	}
	
	function get_report_kh($duration)
	{
	if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_thcat_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam_xe(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy_xe(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua_xe(); break;
			case 'LASTMONTH': 
				return $this->get_report_tong_hop_thang_truoc_xe(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_kh_c($tmp[1],$tmp[2]); break;
		}
	}
	
	function get_report_dt($duration)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_thcat_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam_xe(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy_xe(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua_xe(); break;
			case 'LASTMONTH': 
				return $this->get_report_tong_hop_thang_truoc_xe(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_dt_c($tmp[1],$tmp[2]); break;
		}
	}
	
	function get_report_dtdau_c($bdate,$edate)
	{
		$return = array();
		if($bdate == '' || $edate == '')
		{
			$bdate = date("01/m/Y");
			$edate = date("d/m/Y");
		}
		// Tinh toan so tuan
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$wcount = floor(abs(strtotime($edatet) - strtotime($bdatet))/(7*24*60*60));
		
		//echo $wcount; exit();
		
		// lay tong hop voi cat = 1 - xe đào tạo, chạy xăng / Xe tổng ôn
		$tbc3 = $this->get_listtb_by_cate_type(1,2);
		$tbto = array();
		//khởi tạo mảng
		if($tbc3)
		{
			foreach($tbc3 as $item)
			{
				$tbto[$item['id']]['xe'] = $item;
				for($i = 0;$i < $wcount;$i++)
				{
					$tbto[$item['id']]['nl'][$i] = 0;  
				}
			}
		}
		
		for($i = 0;$i < $wcount;$i++)
		{
			$fbdate = strtotime($bdatet) + $i*7*24*60*60; // 7 ngày tiếp theo
			$fedate = $fbdate + 7*24*60*60; // duration 7 ngày
			$fbdatet = date("d/m/Y",$fbdate);
			$fedatet = date("d/m/Y",$fedate);
			// Lây tổng nhiên liêu sử dụng trong tuần
			$cat3xang = $this->get_report_thcat_c_by_type_cat(1,2,$fbdatet,$fedatet);
			//var_dump($cat3xang);
			if($cat3xang)
			{
				foreach($cat3xang as $item)
				{
					//$tbc3[$item['id']]['nangluong'] = $item['total'];
					$tbto[$item['id']]['nl'][$i] = $item['total'];
					//$tbto[$item['id']][$i] = $tbc3[$item['id']]; 
					//echo $tbc3[$item['id']]['total'];
					//echo $item['total'];
				}
			}
		}
		
		//$cat3dau = $this->get_report_thcat_c_by_type_cat(1,2,$bdate,$edate);
		
		/*if($cat3dau)
		{
			foreach($cat3dau as $item)
			{
				$tbc3[$item['id']]['dau'] = $item['total'];
			}
		}*/
		//var_dump($tbto);		
		$return['dt']= $tbto;
		$return['bdate'] = $bdate;
		// Xử lý ngày kết thúc hết.
		$tmdate = date("d/m/Y",strtotime($edatet) - 24*60*60);
		$return['edate'] = str_replace('-', '/', $tmdate);
		$return['wcount'] = $wcount;
		// Lay so du dau ky
		// 1. Lay so du dau ky thang y
		//var_dump($sddk);
		return $return;
	}
	
	function get_report_thcat_c_by_type_kh($type=1,$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) as amount');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->group_by('o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		if($type != '')
		{
			$this->db->where('o_thiet_bi.type',$type);
		}
		
		$this->db->where('o_thiet_bi.category',3);
		
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
			$this->db->where('o_chung_tu.created_date <',$ngaykt);
		
		$query = $this->db->get();
		//echo $this->db->last_query();
		$rss = $query->result_array();
		if($rss)
		{
			return $rss[0];
		}
		else
		{
			$rss['total'] =0;
			$rss['amount'] = 0;
			return $rss;
		}
	}
	
	function get_report_kh_c($bdate,$edate)
	{
		$return = array();
		if($bdate == '' || $edate == '')
		{
			$bdate = date("01/m/Y");
			$edate = date("d/m/Y");
		}
		// Tinh toan so tuan
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$wcount = floor(abs(strtotime($edatet) - strtotime($bdatet))/(7*24*60*60));
		
		//echo $wcount; exit();
		
		// lay tong hop voi cat = 1 - xe đào tạo, chạy xăng / Xe tổng ôn
		
		$obj = array();
		for($i = 0;$i < $wcount;$i++)
		{
			$fbdate = strtotime($bdatet) + $i*7*24*60*60; // 7 ngày tiếp theo
			$fedate = $fbdate + 7*24*60*60; // duration 7 ngày
			$fbdatet = date("d/m/Y",$fbdate);
			$fedatet = date("d/m/Y",$fedate);
			// Lây tổng nhiên liêu sử dụng trong tuần
			$cat3xang = $this->get_report_thcat_c_by_type_kh(1,$fbdatet,$fedatet);
			//var_dump($cat3xang);
			if($cat3xang)
			{
				$obj[$i] = $cat3xang;
			}
		}
		
		//$cat3dau = $this->get_report_thcat_c_by_type_cat(1,2,$bdate,$edate);
		
		/*if($cat3dau)
		{
			foreach($cat3dau as $item)
			{
				$tbc3[$item['id']]['dau'] = $item['total'];
			}
		}*/
		//var_dump($tbto);		
		$return['kh']= $obj;
		$return['bdate'] = $bdate;
		// Xử lý ngày kết thúc hết.
		$tmdate = date("d/m/Y",strtotime($edatet) - 24*60*60);
		$return['edate'] = str_replace('-', '/', $tmdate);
		$return['wcount'] = $wcount;
		// Lay so du dau ky
		// 1. Lay so du dau ky thang y
		//var_dump($sddk);
		return $return;
	}
	
	function get_report_dt_c($bdate,$edate)
	{
		$return = array();
		if($bdate == '' || $edate == '')
		{
			$bdate = date("01/m/Y");
			$edate = date("d/m/Y");
		}
		// Tinh toan so tuan
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$wcount = floor(abs(strtotime($edatet) - strtotime($bdatet))/(7*24*60*60));
		
		//echo $wcount; exit();
		
		// lay tong hop voi cat = 1 - xe đào tạo, chạy xăng / Xe tổng ôn
		$tbc3 = $this->get_listtb_by_cate_type(1,1);
		$tbto = array();
		//khởi tạo mảng
		if($tbc3)
		{
			foreach($tbc3 as $item)
			{
				$tbto[$item['id']]['xe'] = $item;
				for($i = 0;$i < $wcount;$i++)
				{
					$tbto[$item['id']]['nl'][$i] = 0;  
				}
			}
		}
		
		for($i = 0;$i < $wcount;$i++)
		{
			$fbdate = strtotime($bdatet) + $i*7*24*60*60; // 7 ngày tiếp theo
			$fedate = $fbdate + 7*24*60*60; // duration 7 ngày
			$fbdatet = date("d/m/Y",$fbdate);
			$fedatet = date("d/m/Y",$fedate);
			// Lây tổng nhiên liêu sử dụng trong tuần
			$cat3xang = $this->get_report_thcat_c_by_type_cat(1,1,$fbdatet,$fedatet);
			//var_dump($cat3xang);
			if($cat3xang)
			{
				foreach($cat3xang as $item)
				{
					//$tbc3[$item['id']]['nangluong'] = $item['total'];
					$tbto[$item['id']]['nl'][$i] = $item['total'];
					//$tbto[$item['id']][$i] = $tbc3[$item['id']]; 
					//echo $tbc3[$item['id']]['total'];
					//echo $item['total'];
				}
			}
		}
		
		//$cat3dau = $this->get_report_thcat_c_by_type_cat(1,2,$bdate,$edate);
		
		/*if($cat3dau)
		{
			foreach($cat3dau as $item)
			{
				$tbc3[$item['id']]['dau'] = $item['total'];
			}
		}*/
		//var_dump($tbto);		
		$return['dt']= $tbto;
		$return['bdate'] = $bdate;
		// Xử lý ngày kết thúc hết.
		$tmdate = date("d/m/Y",strtotime($edatet) - 24*60*60);
		$return['edate'] = str_replace('-', '/', $tmdate);
		$return['wcount'] = $wcount;
		// Lay so du dau ky
		// 1. Lay so du dau ky thang y
		//var_dump($sddk);
		return $return;
	}
	
	
	function get_report_ld($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_thcat_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam_xe(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy_xe(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua_xe(); break;
			case 'LASTMONTH': 
				return $this->get_report_tong_hop_thang_truoc_xe(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_ld_c($tmp[1],$tmp[2]); break;
		}
	}
	
	function get_report_ld_c($bdate,$edate)
	{
		$return = array();
		if($bdate == '' || $edate == '')
		{
			$bdate = date("01/m/Y");
			$edate = date("d/m/Y");
		}
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		// lay tong hop voi cat = 2 - xe lãnh đạo
		$tbc3 = $this->get_listtb_by_cate(2);
		$cat3xang = $this->get_report_thcat_c_by_type_cat(2,1,$bdate,$edate);
		$cat3dau = $this->get_report_thcat_c_by_type_cat(2,2,$bdate,$edate);
		if($cat3xang)
		{
			foreach($cat3xang as $item)
			{
				$tbc3[$item['id']]['xang'] = $item['total'];
				//echo $tbc3[$item['id']]['total'];
				//echo $item['total'];
			}
		}
		if($cat3dau)
		{
			foreach($cat3dau as $item)
			{
				$tbc3[$item['id']]['dau'] = $item['total'];
			}
		}
		$return['lanhdao']= $tbc3;
		$return['bdate'] = $bdate;
		// Xử lý ngày kết thúc hết.
		$tmdate = date("d/m/Y",strtotime($edatet) - 24*60*60);
		$return['edate'] = str_replace('-', '/', $tmdate);
		//$return['edate'] = $edate;
		// Lay so du dau ky
		// 1. Lay so du dau ky thang y
		//var_dump($sddk);
		return $return;
	}
	
	function get_report_thcat_c_by_type_c3($type='',$bdate,$edate)
	{
		return $this->get_report_thcat_c_by_type_cat(3,$type,$bdate,$edate);
	}
	function get_report_thcat_c_by_type_cat($cate, $type='',$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('o_thiet_bi.id, SUM(o_chung_tu.ps_giam) AS total');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->group_by('o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		if($type != '')
		{
			$this->db->where('o_thiet_bi.type',$type);
		}
		
		$this->db->where('o_thiet_bi.category',$cate);
		
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
			$this->db->where('o_chung_tu.created_date <',$ngaykt);
		
		$query = $this->db->get();
		//echo $this->db->last_query();
		$rss = $query->result_array();
		if($rss)
		{
			return $rss;
		}
		return null;
	}
	
	// Lay ps_tang trong ky (nhap kho
	function get_ps_tang_xy($bdate,$edate,$type)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('SUM(o_chung_tu.ps_tang) AS total');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id',$type);
		$this->db->where('o_chung_tu.thiet_bi_id', 0);
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
		$this->db->where('o_chung_tu.created_date <=',$ngaykt);
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		//var_dump($rss);
		if($rss)
			return $rss[0]['total'];
		return 0;
	}
	// Lay so du dau ky thang y
	// $type =1 dang; 2 dau
	function get_sddk_y($bdate,$type)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$y = date('Y', strtotime($bdatet));
		$m = date('m', strtotime($bdatet));
		$this->db->select('o_tong_hop.*');
		$this->db->from('o_tong_hop');
		$this->db->where('o_tong_hop.hang_muc_id', $type);
		$this->db->where('YEAR(o_tong_hop.created_date)',$y);
		$this->db->where('MONTH(o_tong_hop.created_date)',$m);
		$query = $this->db->get();
		$rss = $query->result_array();
		if($rss)
			return $rss[0]['sddk'];
		return 0;
	}
	
	function get_ps_xy($bdate,$type)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$ngaybd = date('Y-m-01', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($bdatet));
		
		$this->db->select('SUM(o_chung_tu.ps_giam) AS ps_giam, SUM(o_chung_tu.ps_tang) AS ps_tang');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id',$type);		
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
		$this->db->where('o_chung_tu.created_date <=',$ngaykt);
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		//var_dump($rss);
		if($rss)
			return $rss[0];
		return 0;	
	}
	
	function get_report_thcat_thang()
	{
		$xelanhdaoxang = $this->get_report_thcat_thang_by_type(2,1);
		//var_dump($xelanhdaoxang);		
		//return $rss;
	}
	
	function get_report_thcat_c($bdate,$edate)
	{
		$return = array();
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$lanhdaoxang = $this->get_report_thcat_c_by_type(1,2,$bdate,$edate);
		$lanhdaodau = $this->get_report_thcat_c_by_type(2,2,$bdate,$edate);
		$daotaoxang = $this->get_report_thcat_c_by_type(1,1,$bdate,$edate);
		$daotaodau = $this->get_report_thcat_c_by_type(2,1,$bdate,$edate);
		$return['lanhdao']['xang']= $lanhdaoxang;
		$return['lanhdao']['dau']= $lanhdaodau;
		$return['daotao']['xang']= $daotaoxang;
		$return['daotao']['dau']= $daotaodau;
		
		// lay tong hop voi cat = 3
		$tbc3 = $this->get_listtb_c3();
		$cat3xang = $this->get_report_thcat_c_by_type_c3(1,$bdate,$edate);
		$cat3dau = $this->get_report_thcat_c_by_type_c3(2,$bdate,$edate);
		if($cat3xang)
		{
			foreach($cat3xang as $item)
			{
				$tbc3[$item['id']]['xang'] = $item['total'];
				//echo $tbc3[$item['id']]['total'];
				//echo $item['total'];
			}
		}
		if($cat3dau)
		{
			foreach($cat3dau as $item)
			{
				$tbc3[$item['id']]['dau'] = $item['total'];
			}
		}
		$return['khac']= $tbc3;
		
		// Lay so du dau ky
		// 1. Lay so du dau ky thang y
		$sddkxang = $this->get_sddk_y($bdate,1);
		$sddkdau = $this->get_sddk_y($bdate,2);
		// 2. Lay PS_giam tao so du dau dau ky cua ngay x/y/xxxx
		$ps_giamxang = $this->get_ps_xy($bdate,1);
		$ps_giamdau = $this->get_ps_xy($bdate,2);
		//var_dump($cat3xang);var_dump($tbc3);
		$sddk['xang'] = $sddkxang - $ps_giamxang['ps_giam'] + $ps_giamxang['ps_tang'];
		$sddk['dau'] = $sddkdau - $ps_giamdau['ps_giam'] + $ps_giamdau['ps_tang'];
		$return['sddk'] = $sddk;
		
		// Lay PS tang trong ky
		 $tang['xang']= $this->get_ps_tang_xy($bdate,$edate,1);
		 $tang['dau'] = $this->get_ps_tang_xy($bdate,$edate,2);
		
		$return['ps'] = $tang;
		$return['bdate'] = $bdate;
		// Xử lý ngày kết thúc hết.
		$tmdate = date("d/m/Y",strtotime($edatet) - 24*60*60);
		$return['edate'] = $tmdate;//str_replace('-', '/', $tmdate);
		//$return['edate'] = $edate; 
		//var_dump($sddk);
		return $return;
		//var_dump($lanhdaoxang);
	}
	////////////////////////////////
	function get_report_tong_hop_xe($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_thang_xe(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam_xe(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy_xe(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua_xe(); break;
			case 'LASTMONTH': 
				return $this->get_report_tong_hop_thang_truoc_xe(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_report_tong_hop_xe_c($tmp[1],$tmp[2]); break;
		}
	}
	function get_report_tong_hop_xe_c($bdate,$edate)
	{
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_report_tong_hop_c_xe_hang_muc($item['id'],$bdate,$edate);
			$item['tong'] = $this->tinh_tong_ps_theo_thang_theo_hang_muc($item['id']);
			$item['tongtt'] = $this->tinh_tong_tien_ps_theo_thang_theo_hang_muc($item['id']);
			$tmp[] = $item;
		}
		
		return $tmp;
	}
	
	function get_report_xe_week($duration)
	{
		$tmp = explode("_",$duration);
		$bdate = $tmp[1]; $edate = $tmp[2];
		$this->db->select('o_thiet_bi.*');
		$this->db->from('o_thiet_bi');
		$this->db->where('o_thiet_bi.status',1);
		$this->db->where('o_thiet_bi.kind',1);
		$this->db->or_where('o_thiet_bi.kind',2);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			//$item['dachi'] = $this->get_report_xe_theo_tuan('',$bdate,$edate);
			//$item['dachi'] = $this->get_report_xe_theo_tuan($item['id'],$bdate,$edate);
			//$item['tong'] = $this->tinh_tong_ps_theo_thang_theo_hang_muc($item['id']);
			//$item['tongtt'] = $this->tinh_tong_tien_ps_theo_thang_theo_hang_muc($item['id']);
			$tmp[] = $item;
		}
		
		return $tmp;
	}
	
	function get_report_xe_theo_tuan($thietbiid,$date)
	{
		//$bdatet = str_replace('/', '-', $bdate);
		//$edatet = str_replace('/', '-', $edate);
		//$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngay = date('Y-m-d', strtotime($date));
		//echo $ngaybd.$ngaykt; exit();
		$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname,o_thiet_bi.id as tbid, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		//$this->db->group_by('ngaychi');
		//$this->db->where('o_chung_tu.status', 1);
		//$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		
		
		$this->db->where('o_chung_tu.thiet_bi_id',$thietbiid);
		
		$this->db->where('o_chung_tu.created_date >= ',$ngay);
		$this->db->where('DATE_SUB( o_chung_tu.created_date , INTERVAL 1 DAY ) <',$ngay);
		
		
		//$this->db->where('YEAR(o_chung_tu.created_date)',date('Y',strtotime($date)));
		//$this->db->where('MONTH(o_chung_tu.created_date)',date('m',strtotime($date)));
		//$this->db->where('DAY(o_chung_tu.created_date)',date('d',strtotime($date)));
	
		//$this->db->where('o_chung_tu.created_date <=',$ngaykt);
	
		$query = $this->db->get();
	
		$rss = $query->row();
		//var_dump($rss); exit();
		return $rss;
	}
	
	function get_report_tong_hop_c_xe_hang_muc($hangmucid,$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname,o_thiet_bi.id as tbid, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->group_by('o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		$this->db->where('o_chung_tu.hang_muc_id',$hangmucid);
		
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
			$this->db->where('o_chung_tu.created_date <=',$ngaykt);
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		
		return $rss;
	}
	function get_report_tong_hop_thang_truoc_xe()
	{
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_report_tong_hop_thang_truoc_xe_hang_muc($item['id']);
			$item['tong'] = $this->tinh_tong_ps_theo_thang_theo_hang_muc($item['id']);
			$item['tongtt'] = $this->tinh_tong_tien_ps_theo_thang_theo_hang_muc($item['id']);
			$tmp[] = $item;
		}
		
		return $tmp;
	}
	
	function get_report_tong_hop_thang_truoc_xe_hang_muc($hangmucid)
	{
		$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->group_by('o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		$this->db->where('o_chung_tu.hang_muc_id',$hangmucid);
		if(date('m') != 1)
		{
			$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
			$this->db->where('MONTH(o_chung_tu.created_date)',date('m')-1);
		}
		else {
			$this->db->where('YEAR(o_chung_tu.created_date)',date('Y') - 1);
			$this->db->where('MONTH(o_chung_tu.created_date)',12);
		}
		$query = $this->db->get();
		
		$rss = $query->result_array();
		
		return $rss;
	}
	function get_report_tong_hop_thang_xe()
	{
		/*$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret; */
		
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_report_tong_hop_thang_xe_hang_muc($item['id']);
			$item['tong'] = $this->tinh_tong_ps_theo_thang_theo_hang_muc($item['id']);
			$item['tongtt'] = $this->tinh_tong_tien_ps_theo_thang_theo_hang_muc($item['id']);
			$tmp[] = $item;
		}
		return $tmp;
	}
	
	function get_report_tong_hop_thang_xe_hang_muc($hangmucid)
	{
		$this->db->select('o_chung_tu.*, o_thiet_bi.name as tbname, o_thiet_bi.id as tbid, SUM(o_chung_tu.ps_giam) AS total, SUM(o_chung_tu.ps_giam*o_chung_tu.price) AS amount');
		$this->db->from('o_chung_tu');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->group_by('o_chung_tu.thiet_bi_id');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.thiet_bi_id >', 0);
		$this->db->where('o_chung_tu.hang_muc_id',$hangmucid);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		
		$rss = $query->result_array();
		
		return $rss;
	}
	
	function get_report_tong_hop_nam_xe()
	{
		
	}
	function get_report_tong_hop_quy_xe()
	{
		
	}
	function get_report_tong_hop_nua_xe()
	{
		
	}
	
	
	function get_report_tong_hop($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_thang(); break;
			case 'PREVMONTH':
				return $this->get_report_tong_hop_thang_truoc(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua(); break;
			default :
				return $this->get_report_tong_hop(); break;
		}
		
	}
	function get_report_tong_hop_thang_truoc()
	{
		$this->db->select('o_tong_hop.*, o_hang_muc.name as item, o_thiet_bi.name as tbname');
		$this->db->from('o_tong_hop');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_tong_hop.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_tong_hop.thiet_bi_id', 'left');
		$this->db->order_by('o_tong_hop.id','desc');
		$this->db->where('o_tong_hop.kind', 0);
		if(date('m') -1 == 0 )
		{
			$this->db->where('YEAR(o_tong_hop.created_date)',date('Y')-1);
			$this->db->where('MONTH(o_tong_hop.created_date)',date('m'));
		}
		else
		{
			$this->db->where('YEAR(o_tong_hop.created_date)',date('Y'));
			$this->db->where('MONTH(o_tong_hop.created_date)',date('m')-1);
		}
		//$this->db->where('YEAR(o_tong_hop.created_date)',date('Y'));
	//	$this->db->where('MONTH(o_tong_hop.created_date)',date('m'));
		$query = $this->db->get();		
		
		$rss = $query->result_array();
		
		return $rss;		
/*
		$this->db->select('o_tong_hop.*');
		$this->db->from('o_tong_hop');
		$this->db->where('o_tong_hop.kind', 0);
		if(date('m') -1 == 0 )
		{
			$this->db->where('YEAR(o_tong_hop.created_date)',date('Y')-1);
			$this->db->where('MONTH(o_tong_hop.created_date)',date('m'));
		}
		else
		{
			$this->db->where('YEAR(o_tong_hop.created_date)',date('Y'));
			$this->db->where('MONTH(o_tong_hop.created_date)',date('m')-1);
		}
		$query = $this->db->get();
		return $query->result_array();*/
	}
	
	function get_report_tong_hop_thang()
	{
		$this->db->select('o_tong_hop.*, o_hang_muc.name as item, o_thiet_bi.name as tbname');
		$this->db->from('o_tong_hop');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_tong_hop.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_tong_hop.thiet_bi_id', 'left');
		$this->db->order_by('o_tong_hop.id','desc');
		$this->db->where('o_tong_hop.kind', 0);
		$this->db->where('YEAR(o_tong_hop.created_date)',date('Y'));
		$this->db->where('MONTH(o_tong_hop.created_date)',date('m'));
		$query = $this->db->get();		
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	function get_report_tong_hop_nam()
	{
		return null;
	}
	function get_report_tong_hop_quy()
	{
		return null;
	}
	function get_report_tong_hop_nua()
	{
		return null;
	}
	
	// TÍnh tông PS trong kỳ đối với mỗi hạng mục
	function tinh_tong_ps_theo_hang_muc($hangmucid,$duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->tinh_tong_ps_theo_hang_muc_thang($hangmucid); break;
			case 'THISYEAR':
				return $this->tinh_tong_ps_theo_hang_muc_nam($hangmucid); break;
			case 'QUATER':
				return $this->tinh_tong_ps_theo_hang_muc_quy($hangmucid); break;
			case 'HALF':
				return $this->tinh_tong_ps_theo_hang_muc_nua($hangmucid); break;
			default :
				return $this->tinh_tong_ps_theo_hang_muc_thang($hangmucid); break;
		}
	
	}
	
	function tinh_tong_ps_theo_hang_muc_thang_truoc($hangmucid)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $hangmucid);
		
		if(date('m') -1 == 0 )
		{
			$this->db->where('YEAR(o_chung_tu.created_date)',date('Y')-1);
			$this->db->where('MONTH(o_chung_tu.created_date)',12);
		}
		else
		{
			$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
			$this->db->where('MONTH(o_chung_tu.created_date)',date('m')-1);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_hang_muc_thang($hangmucid)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $hangmucid);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		return $query->result_array();
	}
	function tinh_tong_ps_theo_hang_muc_nam($hangmucid)
	{
		return false;
	}
	function tinh_tong_ps_theo_hang_muc_quy($hangmucid)
	{
		return false;
	}
	function tinh_tong_ps_theo_hang_muc_nua($hangmucid)
	{
		return false;
	}
	
	
	// Tính tổng ps trong kỳ thành tiền
	function tinh_tong_ps_thanh_tien($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->tinh_tong_ps_thanh_tien_theo_thang(); break;
			case 'THISYEAR':
				return $this->tinh_tong_ps_thanh_tien_theo_thang(); break;
			case 'QUATER':
				return $this->tinh_tong_ps_thanh_tien_theo_thang(); break;
			case 'HALF':
				return $this->tinh_tong_ps_thanh_tien_theo_thang(); break;
			case 'YESTERDAY':
				return $this->tinh_tong_ps_thanh_tien_theo_thang(); break;
			default :
				return $this->tinh_tong_ps_thanh_tien_theo_thang(); break;
		}
	
	}
	

	function tinh_tong_ps_thanh_tien_theo_thang()
	{
		$this->db->select('SUM(ps_tang*price) as tang, SUM(ps_giam*price) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
	
		return $query->result_array();
	}
	
	// Tính tổn PS trong kỳ
	function tinh_tong_ps($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->tinh_tong_ps_theo_thang(); break;
			case 'THISYEAR':
				return $this->tinh_tong_ps_theo_nam(); break;
			case 'QUATER':
				return $this->tinh_tong_ps_theo_quy(); break;
			case 'HALF':
				return $this->tinh_tong_ps_theo_nua(); break;
			case 'YESTERDAY':
				return $this->tinh_tong_ps_theo_homqua(); break;
			default :
				return $this->tinh_tong_ps_theo_thang(); break;
		}
		
	}
	
	function tinh_tong_ps_theo_thang()
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_thang_theo_hang_muc($id)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
	
		return $query->result_array();
	}
	
	function tinh_tong_tien_ps_theo_thang_theo_hang_muc($id)
	{
		$this->db->select('SUM(ps_tang*price) as tang, SUM(ps_giam*price) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
	
		return $query->result_array();
	}
	
	
	function tinh_tong_ps_theo_nam()
	{
		return null;
	}
	function tinh_tong_ps_theo_quy()
	{
		return null;
	}
	function tinh_tong_ps_theo_nua()
	{
		return null;
	}
	function tinh_tong_ps_theo_homqua()
	{
		return null;
	}
	function update_o_tong_hop($id,$ps_tang,$ps_giam,$ck)
	{

		$fields = array(
				'ps_tang'			=> $ps_tang,
				'ps_giam'			=> $ps_giam,
				'sdck'=>$ck
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_tong_hop');
	}
	
	function delete_chung_tu($id)
	{
		$fields = array(
				'status'			=> 99,
				'created_date' =>$datetime = date('Y-m-d H:i:s')
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_chung_tu');
	}
	function submit_chung_tu($id)
	{
		$fields = array(
				'status'			=> 1,
				'created_date' =>$datetime = date('Y-m-d H:i:s')
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_chung_tu');
	}
	function save_chung_tu($id,$ps_tang,$ps_giam)
	{
		$ps_tang = str_replace(',','',$ps_tang); //clean ,
		$ps_giam = str_replace(',','',$ps_giam);
		$fields = array(
				'ps_tang'			=> $ps_tang,
				'ps_giam'			=> $ps_giam
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->where('status', 0);
		return $this->db->update('o_chung_tu');
	}
	
	function themchungtu($hang_muc_id,$ps_tang,$ps_giam,$note,$price,$thiet_bi_id)
	{
		$ps_tang = str_replace(',','',$ps_tang); //clean ,
		$ps_giam = str_replace(',','',$ps_giam);
		$fields = array(
				'hang_muc_id'			=> $hang_muc_id,
				'ps_tang'	=>$ps_tang,
				'ps_giam'		=> $ps_giam,
				'note'=>$note,
				'price'=>$price,
				'status' =>0,
				'thiet_bi_id' =>$thiet_bi_id
		);
		
		$this->db->set($fields);
		$this->db->insert('o_chung_tu');
		return $this->db->insert_id();
	}
	// Get dvt
	function get_dvt($hang_muc_id)
	{
		$this->db->select('o_hang_muc.*, dvt.name as dvt');
		$this->db->from('o_hang_muc');
		$this->db->join('dvt', 'dvt.id = o_hang_muc.dvt_id', 'left');
		$this->db->where('o_hang_muc.id',$hang_muc_id);
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function get_all_chung_tu_ok($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_all_chung_tu_ok_theo_thang(); break;
			case 'LASTMONTH':
					return $this->get_all_chung_tu_ok_theo_thang_truoc(); break;
			case 'THISYEAR':
				return $this->get_all_chung_tu_ok_theo_nam(); break;
			case 'QUATER':
				return $this->get_all_chung_tu_ok_theo_quy(); break;
			case 'HALF':
				return $this->get_all_chung_tu_ok_theo_nua(); break;
			case 'YESTERDAY':
				return $this->get_all_chung_tu_ok_theo_homqua(); break;
			case 'TODAY':
					return $this->get_all_chung_tu_ok_theo_today(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_all_chung_tu_ok_theo_c($tmp[1],$tmp[2]); break;
				
		}
	
	}
	
	function get_all_chung_tu_ok_theo_today()
	{
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_all_chung_tu_ok_today_don_vi_nhan($item['id']);
			$item['tong'] = $this->tinh_tong_ps_theo_today_hang_muc($item['id']);
			$item['tongtt'] = $this->tinh_tong_tien_ps_today_theo_hang_muc($item['id']);
			$tmp[] = $item;
		}
		//var_dump($tmp);exit();
		return $tmp;
	}
	
	function get_all_chung_tu_ok_today_don_vi_nhan($id)
	{
		$this->db->select('o_chung_tu.*, o_hang_muc.name as item, o_thiet_bi.code as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_chung_tu.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$this->db->where('DAY(o_chung_tu.created_date)',date('d'));
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_today_hang_muc($id)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$this->db->where('DAY(o_chung_tu.created_date)',date('d'));
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function tinh_tong_tien_ps_today_theo_hang_muc($id)
	{
		$this->db->select('SUM(ps_tang*price) as tang, SUM(ps_giam*price) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$this->db->where('DAY(o_chung_tu.created_date)',date('d'));
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function get_all_chung_tu_ok_theo_c($bdate,$edate)
	{
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_all_chung_tu_ok_theo_c_don_vi_nhan($item['id'],$bdate,$edate);
			$item['tong'] = $this->tinh_tong_ps_theo_c_theo_hang_muc($item['id'],$bdate,$edate);
			$item['tongtt'] = $this->tinh_tong_tien_ps_theo_c_theo_hang_muc($item['id'],$bdate,$edate);
			$tmp[] = $item;
		}
		//var_dump($tmp);exit();
		return $tmp;
	}
	
	function get_all_chung_tu_ok_theo_c_don_vi_nhan($id,$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('o_chung_tu.*, o_hang_muc.name as item, o_thiet_bi.code as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_chung_tu.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);	
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
		$this->db->where('DATE_SUB( o_chung_tu.created_date , INTERVAL 1 DAY ) <',$ngaykt);
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		
		return $rss;
	}
	function tinh_tong_ps_theo_c_theo_hang_muc($id,$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
		$this->db->where('DATE_SUB( o_chung_tu.created_date , INTERVAL 1 DAY ) <=',$ngaykt);
		
		
		$query = $this->db->get();
		
		return $query->result_array();
	}
	function tinh_tong_tien_ps_theo_c_theo_hang_muc($id,$bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		$this->db->select('SUM(ps_tang*price) as tang, SUM(ps_giam*price) as giam');
		$this->db->from('o_chung_tu');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('o_chung_tu.created_date >= ',$ngaybd);
		$this->db->where('DATE_SUB( o_chung_tu.created_date , INTERVAL 1 DAY ) <=',$ngaykt);
		
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function get_all_chung_tu_ok_theo_thang_truoc()
	{
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		$edate = date('01-m-Y');//date("t-m-Y", strtotime("first day of previous month") );
		$bdate = date("01-m-Y", strtotime("first day of previous month") );
		//echo $bdate; exit();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_all_chung_tu_ok_theo_c_don_vi_nhan($item['id'],$bdate,$edate);
			$item['tong'] = $this->tinh_tong_ps_theo_c_theo_hang_muc($item['id'],$bdate,$edate);
			$item['tongtt'] = $this->tinh_tong_tien_ps_theo_c_theo_hang_muc($item['id'],$bdate,$edate);
			$tmp[] = $item;
		}
		//var_dump($tmp);exit();
		return $tmp;
	}
	
	function get_all_chung_tu_ok_theo_thang()
	{
		/*$this->db->select('o_chung_tu.*, o_hang_muc.name as item, o_thiet_bi.code as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_chung_tu.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		
		$query = $this->db->get();

		return $query->result_array();
		*/
		
		$this->db->select('o_hang_muc.*');
		$this->db->from('o_hang_muc');
		$this->db->where('o_hang_muc.status',1);
		$query = $this->db->get();
		$return = $query->result_array();
		$tmp = array();
		//var_dump($return);exit();
		foreach($return as $item)
		{
			$item['chungtus'] = $this->get_all_chung_tu_ok_theo_thang_don_vi_nhan($item['id']);
			$item['tong'] = $this->tinh_tong_ps_theo_thang_theo_hang_muc($item['id']);
			$item['tongtt'] = $this->tinh_tong_tien_ps_theo_thang_theo_hang_muc($item['id']);
			$tmp[] = $item;
		}
		//var_dump($tmp);exit();
		return $tmp;
	}
	
	function get_all_chung_tu_ok_theo_thang_don_vi_nhan($id)
	{
		$this->db->select('o_chung_tu.*, o_hang_muc.name as item, o_thiet_bi.code as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_chung_tu.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status', 1);
		$this->db->where('o_chung_tu.hang_muc_id', $id);
		$this->db->where('YEAR(o_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(o_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_chung_tu_ok_theo_nam()
	{
		return null;
	}
	function get_all_chung_tu_ok_theo_quy()
	{
		return null;
	}
	function get_all_chung_tu_ok_theo_nua()
	{
		return null;
	}
	function get_all_chung_tu_ok_theo_homqua()
	{
		return null;
	}
	// Lây tất cả chứng từ trong vòng 3 tháng
	function get_all_chung_tu()
	{
		$this->db->select('o_chung_tu.*, o_hang_muc.name as item, dvt.name as dvt, o_thiet_bi.code as tbname');
		$this->db->from('o_chung_tu');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_chung_tu.hang_muc_id', 'left');
		$this->db->join('o_thiet_bi', 'o_thiet_bi.id = o_chung_tu.thiet_bi_id', 'left');
		$this->db->join('dvt', 'dvt.id = o_hang_muc.dvt_id', 'left');
		$this->db->where('o_chung_tu.created_date < NOW()');
		$this->db->where('o_chung_tu.created_date > DATE_ADD(NOW(),INTERVAL-3 MONTH)');
		$this->db->order_by('o_chung_tu.id','desc');
		$this->db->where('o_chung_tu.status < 99');
		$this->db->limit(50);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Edit SDDK
	
	function submitsddk($id)
	{
		$fields = array(
				'status'			=> 1
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_tong_hop');
	}
	function saveiteminstore($id,$amount,$price = 0)
	{
		$fields = array(
				'sddk'			=> $amount,
				'price'			=> $price
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_tong_hop');
	}
	
	// Nhập số dư đầu kỳ cho các hạng mục
	function nhap_do_du_dau_ky( $hangmucid,$sddk,$price = 0)
	{
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>0,
				'ps_giam'		=> 0,
				'sddk' =>$sddk,
				'status' =>0,
				'kind' =>1,
				'price' =>$price
		);
		
		$this->db->set($fields);
		return $this->db->insert('o_tong_hop');
	}
	
	// Lây danh sách do du dau ky khởi tạo
	function get_all_sddk()
	{
		$this->db->select('o_tong_hop.*, o_hang_muc.name as item, dvt.name as dvt');
		$this->db->from('o_tong_hop');
		$this->db->join('o_hang_muc', 'o_hang_muc.id = o_tong_hop.hang_muc_id', 'left');
		$this->db->join('dvt', 'dvt.id = o_hang_muc.dvt_id', 'left');
		$this->db->where('kind', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	//tự động chạy khi ngày đầu tiên của tháng tiếp theo
	function create_tong_hop()
	{
		//1. Lấy ra danh sách hạng mục
		$categories = $this->get_all_category(1);
		
	}
	
	function report_tong_hop()
	{
		
	}
	
	// Phát sinh trong kỳ
	function report_chi_tiet()
	{
		
	}
	
	// Nhập phiêu chi
	function nhap_phieu_chi($hangmucid,$psgiam)
	{
		return $this->add_chung_tu($hangmucid, 0, $psgiam);
	}
	// NHập phiêu thu
	function nhap_phieu_thu($hangmucid,$pstang)
	{
		return $this->add_chung_tu($hangmucid, $pstang, 0);
	}
	// Nhập dữ liệu vào bảng o_chung_tu
	function add_chung_tu($hangmucid,$pstang,$psgiam)
	{
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>$pstang,
				'ps_giam'		=> $psgiam,
				'status' =>0
		);
	
		$this->db->set($fields);
		return $this->db->insert('o_chung_tu');
	}
	
	
	// START THIET BI
	
	function submit_thiet_bi($id)
	{
		$fields = array(
				'status'		=> 1,
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_thiet_bi');
	}
	
	function get_all_thietbi($status = '',$type='',$category='')
	{
		$this->db->select('*');
		$this->db->from('o_thiet_bi');
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		if($type != "")
		{
			$this->db->where('type',$type);
		}
		if($category != "")
		{
			$this->db->where('category',$category);
		}
		$this->db->order_by('id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function getthietbibycode($code)
	{
		$this->db->select('*');
		$this->db->from('o_thiet_bi');
		$this->db->where('code',$code);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// add thiet bi
	function themthietbi($name,$code,$description,$type=0,$category=0)
	{
		$fields = array(
				'name'			=> $name,
				'description'	=> $description,
				'code' => $code,
				'type'=>$type,
				'category'=>$category,
				'status'		=> 0,
		);
		
		$this->db->set($fields);
		$this->db->insert('o_thiet_bi');
		return $this->db->insert_id();
		
	}
	//- END THIET BI
	
	// START o_hang_muc
	
	function add_sddk($hangmucid,$sddk)
	{
	
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>0,
				'sddk'	=>$sddk,
				'ps_giam'		=> 0,
				'status' =>0,
				'kind' =>1,
				'thiet_bi_id'=>0
		);
		$this->db->set($fields);
		return $this->db->insert('o_tong_hop');
	
	}
	// count all category
	function get_all_category($status = '')
	{
		$this->db->select('*');
		$this->db->from('o_hang_muc');
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		$this->db->order_by('id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// add category
	function add_category($name,$description)
	{
		$fields = array(
				'name'			=> $name,
				'description'	=> $description,
				'status'		=> 0,
				'dvt_id' =>7
		);
	
		$this->db->set($fields);
		return $this->db->insert('o_hang_muc');
	}
	
	// Edit category
	public function edit_category($id)
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'description'			=> mb_strtoupper($this->input->post('description'))
		);
	
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('o_hang_muc');
	}
	
	
	// get category
	function get_category($id)
	{
		$this->db->select('*');
		$this->db->from('o_hang_muc');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function submit_hang_muc($id)
	{
		$fields = array(
				'status'		=> 1
		
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('o_hang_muc');
	}
	// END CATEGORY CONTROLLER
	
	function doit()
	{
		$this->db->select('*');
		$this->db->from('o_thiet_bi');
		$this->db->where('kind', 1);
		$this->db->or_where('kind', 3);
		$query = $this->db->get();
		$rss = $query->result_array();
		
		return $rss;
	}
}

/* Location: ./application/models/bangdiem_model.php */